/*
* jlb
*/
package com.jlb.news.ctrl;

import com.alibaba.fastjson.JSON;
import com.jlb.base.ctrl.AppBaseCtrl;
import com.jlb.core.entity.BeanRet;
import com.jlb.core.entity.Page;
import com.jlb.core.tools.redis.RedisUtils;
import com.jlb.news.entity.News;
import com.jlb.news.entity.NewsState;
import com.jlb.news.service.NewsSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新闻公告 控制器
 *
 * @author jlb
 */
@RestController
@RequestMapping("/app/news")
@Slf4j
@Api(value = "新闻公告控制器", description = "用户端：新闻公告控制器")
public class AppNewsCtrl extends AppBaseCtrl {

    @Resource
    protected RedisUtils redisUtils;

    @Resource
    private NewsSV newsSV;

    /**
     * 查询News一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询News一个详情信息", notes = "查询News一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null && code == null) {
            return null;
        }
        News news = newsSV.load(id, code);
        log.info(JSON.toJSONString(news));
        return BeanRet.create(true, "查询成功", news);
    }


    /**
     * 查询News信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询News信息集合", notes = "查询新闻信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "Authorization", value = "Authorization", paramType = "query")
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore News news, Integer curPage, Integer pageSize) {
        Page<News> page = new Page<News>(pageSize, curPage);
        news.setState(NewsState.Released.name());
        List<News> newss = newsSV.list(news, page.genRowStart(), page.getPageSize());
        page.setVoList(newss);
        int total = newsSV.count(news);
        page.setTotalRow(total);

        return BeanRet.create(true, "查询成功", page);

    }

}
